namespace('base.department');
/**
 * 初始化菜单设置主页面
 */
base.department.init = function(){
	var selectedDepartmentId = '';
	var setting = {
		async: {
			enable: true,
			url: 'base/department/department!findSubDepartment.action',
			autoParam:['id']
		},
		data: {
			key: {
				url: 'xUrl'
			}
		},
		callback: {
			onClick: function(e, treeViewId, node){
				selectedDepartmentId = node.id==null?'':node.id;
				if(selectedDepartmentId==''){
					// 根节点不可编辑;
					return;
				}
				$('#departmentEditPanel').load(
					'base/department/department!preUpdate.action?id=' + node.id,
					function(){
						base.department.initDepartmentEditPanel();
					}
				);
				return false;
			},
			onAsyncError: function(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown){
				alert('Error occurred while getting sub department');
			}
		}
	};
	var root = [{
		id: $('#rootDepartmentId').val(),
		name: $('#rootDepartmentName').val(),
		open: true,
		isParent: true
	}];
	// 构建树;
	var zTree = $.fn.zTree.init($("#departmentTreeView"), setting, root);
	
	// 获得并展开根节点;
	var rootNode = zTree.getNodes()[0];
	zTree.reAsyncChildNodes(rootNode, 'refresh', false);

	$('#btnAddDepartment').click(function(){
		var dimensionId = $('#dimensions').val();
		$('#departmentEditPanel').load(
			'base/department/department!preAdd.action?dimensionId='+ dimensionId + '&id=' + selectedDepartmentId,
			function(){
				base.department.initDepartmentEditPanel();
			}
		);
	});

	$('#btnDeleteDepartment').click(function(){
		if(selectedDepartmentId==''){
			alert('您还没有选中任何节点');
			return;
		}
		$('#modalConfirmDeleteDepartment').modal({});
	});

	$('#btnConfirmDeleteDepartment').click(function(){
		$.getJSON(
			'base/department/department!delete.action?id=' + selectedDepartmentId + '&t=' + new Date(), 
			function(result){
				$('#modalConfirmDeleteDepartment').modal('hide');
				if(result.success){
					zTree.reAsyncChildNodes(rootNode, 'refresh', false);
				}
				else{
					alert(result.message);
				}
			}
		);
	});
	
	$('#btnAddEmployee').click(function(){
		if(selectedDepartmentId==''){
			alert('你还没有选中任何部门');
			return;
		}
		$('#departmentEditPanel').load('base/employee/employee!preAdd.action?departmentId=' + selectedDepartmentId, function(){
			base.department.initEmployeeEditPanel();
		});
	});
	
	$('#dimensions').change(function(){
		var dimensionId = $(this).val();
		toLocation('base/department/department.action?dimensionId=' + dimensionId);
	});
};

/**
 * 初始化部门添加/编辑页面，使之ajaxForm
 */
base.department.initDepartmentEditPanel = function(){
	$('#departmentEditForm').ajaxForm({
        target: '#departmentEditPanel',
        success: function(){
            base.department.reloadDepartmentTree();
            base.department.initDepartmentEditPanel();
        }
    });
};

/**
 * 刷新部门树
 */
base.department.reloadDepartmentTree = function(){
	var zTree = $.fn.zTree.getZTreeObj("departmentTreeView");
	var rootNode = zTree.getNodes()[0];
	zTree.reAsyncChildNodes(rootNode, 'refresh', false);
};

/**
 * 初始化人员添加/编辑页面
 */
base.department.initEmployeeEditPanel = function(){
	$('#employeeEditForm').ajaxForm({
		target: '#departmentEditPanel',
		success: function(){
			//alert('Employee added successfully');
		}
	});
};